var TechnologyTableList = {//医生耗材表格初始化
    id: "TechnologyTableList",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

TechnologyTableList.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,width:110},
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true,width:90},
        {title: '耗材', field: 'consumableName', align: 'center', valign: 'middle', sortable: true,width:90},
        {title: '数量', field: 'number', align: 'center', valign: 'middle', sortable: true,width:80},
        {title: '单价', field: 'price', align: 'center', valign: 'middle', sortable: true,width:80},
        {title: '金额', field: 'amount', align: 'center', valign: 'middle', sortable: true,width:80},
        {title: '实付金额', field: 'amountActually', align: 'center', valign: 'middle', sortable: true,width:110},
        {title: '所属类别', field: 'type', align: 'center', valign: 'middle', sortable: true,width:110},
        {title: '门店', field: 'arrivalStore', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true,width:170},
        {
            title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                var ids = row.id
                return '<button title="编辑" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;" onclick="TechnologyTableList.updateTec(' + "'" + ids + "'" + ')" type="button" class="btn"><span class="glyphicon glyphicon-edit"></span></button> ' +
                    '<button title="删除" onclick="TechnologyTableList.deleteTec(' + "'" + ids + "'" + ')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>'
            }
        },
    ];
    return columns;
};

TechnologyTableList.check = function () {// 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        TechnologyTableList.seItem = selected[0];
        return true;
    }
};

TechnologyTableList.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};

TechnologyTableList.updateTec = function (ids) {//编辑耗材加工
    var index = layer.open({
        type: 2,
        title: '编辑',
        area: ['70%', '65%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/update_tec/' + ids
    });
    this.layerIndex = index;
};

TechnologyTableList.deleteTec = function (ids) {//删除
    $.ajax({
        url: Feng.ctxPath + "/doctorLooked/delete_tec",
        type: "post",
        data: {
            ids: ids
        },
        success: function (data) {
            Feng.success('操作成功!');
            window.TechnologyTableList.table.refresh();
        },
        error: function (data) {
            alert('没有权限，请联系管理员!');
        }
    });
};

TechnologyTableList.search = function () {//查询耗材列表记录
    TechnologyTableList.table.refresh();
};

$(function () {
    var defaultColunms = TechnologyTableList.initColumn();
    var reservationsId = $("#reservationsId").val();
    var table = new BSTable(TechnologyTableList.id, "/doctorLooked/technologyTableList/" + reservationsId, defaultColunms);
    table.setPaginationType("client");//客户端分页
    TechnologyTableList.table = table.init();
    TechnologyTableList.search();
    if($("#tabId").val() == 7){
        $('#TechnologyTableList').bootstrapTable('resetView', {height: $(window).height() - 105});
    };

    $("#addWay").click(function () {//添加
        var mode = $(this).parent().children().eq(0).clone();
        $(mode).find('.leftUp,.rightUp,.rightDown,.leftDown,.selectStyles').val("");
        $(this).parent().append(mode)
    });

    $("#addWay1").click(function () {//添加
        var mode = $(this).parent().children().eq(0).clone();
        $(mode).find('.leftUp1,.rightUp1,.rightDown1,.leftDown1,.selectStyles').val("");
        $(this).parent().append(mode)
    });

    $("#machiningBox1").on('click','.delete1',function () {
        if($("#machiningBox1").children('.historyBox').length > 1){
            $(this).parent().remove()
        }
    });

    $("#machiningBox").on('click','.delete',function () {
        if($("#machiningBox").children('.historyBox').length > 1){
            $(this).parent().remove()
        }
    });

    $("#ensure").click(function () {//耗材新增确定
        var customerId=$("#customerId").val();
        var reservationsId=$("#reservationsId").val();
        var doctor =$("#doctors").val();
        var remarks =$("#remarkss").val();
        var str = [];
        var endtr = "";
        for(var i=0;i< $(".consumableName").length;i++){
            if($(".consumableName").eq(i).val()==""||$(".consumableName").eq(i).val()==null){
                alert("耗材不能为空");
                return;
            }
            if($(".arrivalStore").eq(i).val()==""||$(".arrivalStore").eq(i).val()==null){
                alert("门店不能为空");
                return;
            }
            if($(".number").eq(i).val()==""||$(".number").eq(i).val()==null){
                alert("数量不能为空");
                return;
            }
            if($(".price").eq(i).val()==""||$(".price").eq(i).val()==null){
                alert("单价不能为空");
                return;
            }
            if($(".amount").eq(i).val()==""||$(".amount").eq(i).val()==null){
                alert("金额不能为空");
                return;
            }
            if($(".amountActually").eq(i).val()==""||$(".amountActually").eq(i).val()==null){
                alert("实付金额不能为空");
                return;
            }
            if($(".type").eq(i).val()==""||$(".type").eq(i).val()==null){
                alert("所属类别不能为空");
                return;
            }
            endtr =$(".leftUp").eq(i).val()+"#"+$(".rightUp").eq(i).val()+"#"+
                $(".rightDown").eq(i).val()+"#"+ $(".leftDown").eq(i).val()+"#"+
                $(".consumableName").eq(i).val()+"#"+$(".arrivalStore").eq(i).val()+"#"+$(".number").eq(i).val()+"#"+$(".price").eq(i).val()+"#"+$(".amount").eq(i).val()+"#"+$(".amountActually").eq(i).val()+"#"+$(".type").eq(i).val();
            str.push(endtr);
        }
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/ensure",
            type: "post",
            traditional: true,
            data: {
                "customerId": customerId,
                "reservationsId": reservationsId,
                "str": str,
                "doctor":doctor,
                "remarks":remarks
            },
            success: function (data) {
                Feng.success("添加成功！");
                window.location.href= Feng.ctxPath + '/doctorLooked/doctor_seeDiagnosis/' + customerId+'/'+7
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    $("#ensure1").click(function () {//机加工新增确定
        var customerId=$("#customerId").val();
        var reservationsId=$("#reservationsId").val();
        var processUnit = $("#processUnit").val();
        var status = $("#status").val();
        var doctor = $("#doctor1").val();
        var orderNumber = $("#orderNumber").val();
        var picker = $("#picker").val();
        var phone = $("#phone").val();
        var deliveryTime = $("#deliveryTime").val();
        var pickUpTime = $("#pickUpTime").val();
        var remarks = $("#remarks1").val();
        var store = $("#store").val();
        var str = [];
        var endtr = "";
        if(doctor==null || doctor=="" ){
            alert("医生不能为空");
            return;
        }
        if(store==null || store=="" ){
            alert("门诊不能为空");
            return;
        }
        for(var i=0;i< $(".payServiceNameNew").length;i++){
            if($(".payServiceNameNew").eq(i).val()==""||$(".payServiceNameNew").eq(i).val()==null){
                alert("耗材不能为空");
                return;
            }
            if($(".number1").eq(i).val()==""||$(".number1").eq(i).val()==null){
                alert("数量类别不能为空");
                return;
            }
            if($(".amount1").eq(i).val()==""||$(".amount1").eq(i).val()==null){
                alert("金额不能为空");
                return;
            }
            if($(".amountActually1").eq(i).val()==""||$(".amountActually1").eq(i).val()==null){
                alert("实付金额不能为空");
                return;
            }
            if($(".price1").eq(i).val()==""||$(".price1").eq(i).val()==null){
                alert("单价不能为空");
                return;
            }
            if($(".type1").eq(i).val()==""||$(".type1").eq(i).val()==null){
                alert("类别不能为空");
                return;
            }
            endtr =$(".leftUp1").eq(i).val()+"#"+$(".rightUp1").eq(i).val()+"#"+
                $(".rightDown1").eq(i).val()+"#"+ $(".leftDown1").eq(i).val()+"#"+
                $(".payServiceNameNew").eq(i).val()+"#"+$(".number1").eq(i).val()+"#"+$(".amount1").eq(i).val()+"#"+$(".amountActually1").eq(i).val()+"#"+$(".price1").eq(i).val()+"#"+$(".type1").eq(i).val();
            str.push(endtr);
        }
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/ensure1",
            type: "post",
            traditional: true,
            data: {
                "customerId": customerId,
                "reservationsId": reservationsId,
                "str": str,
                "doctor":doctor,
                "store":store,
                "remarks":remarks,
                "processUnit":processUnit,
                "orderNumber":orderNumber,
                "picker":picker,
                "phone":phone,
                "deliveryTime":deliveryTime,
                "pickUpTime":pickUpTime,
                "status":status
            },
            success: function (data) {
                Feng.success("添加成功！");
                window.location.href= Feng.ctxPath + '/doctorLooked/doctor_seeDiagnosis/' + customerId+'/'+7;
            },
            error: function (data) {
                alert('没有权限，请联系管理员!');
            }
        });
    });

    consumablesNumber = function(obj) {//耗材新增数量
        var price = $(obj).parent().siblings().children('.price').val();
        var amount = parseInt(price) * parseInt($(obj).val())
        if(isNaN(amount) != true){
            $(obj).parent().siblings().children('.amount').val(amount);
            $(obj).parent().siblings().children('.amountActually').val(amount);
        }
        var totalSum = 0;
        $("#machiningBox .amount").each(function (index, obj) {
            if($(obj).val() != ""){
                totalSum += parseInt($(obj).val());
            }
        });
        $("#consumablesTotal").html(totalSum);
    };

    consumablesPrice = function (obj) {//耗材新增单价
        $(obj).val($(obj).val().replace(/[^\d]/g, ''));
        var num = $(obj).parent().siblings().children('.number').val();
        var amount = parseInt(num) * parseInt($(obj).val())
        if(isNaN(amount) != true){
            $(obj).parent().siblings().children('.amount').val(amount);
            $(obj).parent().siblings().children('.amountActually').val(amount);
        }
        var totalSum = 0;
        $("#machiningBox .amount").each(function (index, obj) {
            if($(obj).val() != ""){
                totalSum += parseInt($(obj).val());
            }
        });
        $("#consumablesTotal").html(totalSum);
    };

    consumablesActually = function (obj) {
        if(isNaN($(obj).val()) != true){
            var totalSum = 0;
            $("#machiningBox .amountActually").each(function (index, obj) {
                if($(obj).val() != ""){
                    totalSum += parseInt($(obj).val());
                }
            });
            $("#consumablesTotal").html(totalSum);
        }
    };

    machiningNumber = function(obj) {//机加工新数量
        $(obj).val($(obj).val().replace(/[^\d]/g, ''));
        var price = $(obj).parent().siblings().children('.price1').val();
        var amount1 = parseInt(price) * parseInt($(obj).val())
        if(isNaN(amount1) != true){
            $(obj).parent().siblings().children('.amount1').val(amount1);
            $(obj).parent().siblings().children('.amountActually1').val(amount);
        }
        var totalSum = 0;
        $("#machiningBox1 .amount1").each(function (index, obj) {
            if($(obj).val() != ""){
                totalSum += parseInt($(obj).val());
            }
        });
        $("#machiningTotal").html(totalSum);
    };

    machiningPrice = function(obj) {//机加工新单价
        $(obj).val($(obj).val().replace(/[^\d]/g, ''));
        var num = $(obj).parent().siblings().children('.number1').val();
        var amount1 = parseInt(num) * parseInt($(obj).val())
        if(isNaN(amount1) != true){
            $(obj).parent().siblings().children('.amount1').val(amount1);
            $(obj).parent().siblings().children('.amountActually1').val(amount1);
        }
        var totalSum = 0;
        $("#machiningBox1 .amount1").each(function (index, obj) {
            if($(obj).val() != ""){
                totalSum += parseInt($(obj).val());
            }
        });
        $("#machiningTotal").html(totalSum);
    };

    machiningActually = function (obj) {
        if(isNaN($(obj).val()) != true){
            var totalSum = 0;
            $("#machiningBox1 .amountActually1").each(function (index, obj) {
                if($(obj).val() != ""){
                    totalSum += parseInt($(obj).val());
                }
            });
            $("#machiningTotal").html(totalSum);
        }
    };

    var index;
    $(".machiningBox").on('click', '.toothPosition', function (e) {
        e.stopPropagation();
        $(".toothPosition").removeClass('tips');
        $(this).addClass('tips');
        var leftUpNum = $(this).find('.leftUp').val();
        var rightUpNum = $(this).find('.rightUp').val();
        var leftDownNum = $(this).find('.leftDown').val();
        var rightDownNum = $(this).find('.rightDown').val();
        index = layer.open({
            type: 4,
            tips: [1, '#ffffff'],
            closeBtn: 0,
            area: ['auto', 'auto'],
            content: ['<div>' + $("#noneBox").html() + '</div>', '.tips'],
            success : function(layero, index){
                if(leftUpNum.length > 0){
                    for(var w=0 ; w<$(layero).find('.toothTopLeft>span').length ; w++){
                        for(var i=0 ; i<leftUpNum.length ; i++){
                            if($(layero).find('.toothTopLeft>span').eq(w).find('span').text() == leftUpNum.substring(i,i+1)){
                                $(layero).find('.toothTopLeft>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }
                if(rightUpNum.length > 0){
                    for(var w=0 ; w<$(layero).find('.toothTopRight>span').length ; w++){
                        for(var i=0 ; i<rightUpNum.length ; i++){
                            if($(layero).find('.toothTopRight>span').eq(w).find('span').text() == rightUpNum.substring(i,i+1)){
                                $(layero).find('.toothTopRight>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }
                if(leftDownNum.length > 0){
                    for(var w=0 ; w<$(layero).find('.toothBottomLeft>span').length ; w++){
                        for(var i=0 ; i<leftDownNum.length ; i++){
                            if($(layero).find('.toothBottomLeft>span').eq(w).find('span').text() == leftDownNum.substring(i,i+1)){
                                $(layero).find('.toothBottomLeft>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }
                if(rightDownNum.length > 0){
                    for(var w=0 ; w<$(layero).find('.toothBottomRight>span').length ; w++){
                        for(var i=0 ; i<rightDownNum.length ; i++){
                            if($(layero).find('.toothBottomRight>span').eq(w).find('span').text() == rightDownNum.substring(i,i+1)){
                                $(layero).find('.toothBottomRight>span').eq(w).find('span').addClass('spanStyle');
                            }
                        }
                    }
                }
            }
        });
    });

    $(document).on('mouseleave','#tipsBox',function () {
        layer.close(index);
    });

    $(document).on('click', '.tipsUl>li', function (e) {
        e.stopPropagation();
        $(this).addClass('tipsLi').siblings().removeClass('tipsLi');
        if ($(this).index() == 0) {
            $(this).parent().siblings().children('#permanent').show().siblings().hide();
        } else if ($(this).index() == 1) {
            $(this).parent().siblings().children('#deciduous').show().siblings().hide();
        }
    });

    $(document).on('click', '.toothTopLeft>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .leftUp").val().replace(text, "");
            for(var i=0 ; i<inputValue.length ; i++){
                arr.push(inputValue.substring(i,i+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]>arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change;
                    }
                }
            };
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(".tips .leftUp").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            var add = $(".tips .leftUp").val() + text;
            for(var d=0 ; d<add.length ; d++){
                arr.push(add.substring(d,d+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]>arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change;
                    }
                }
            };
            console.log(arr);
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(this).children().addClass('spanStyle');
            $(".tips .leftUp").val(str);
        }
    });

    $(document).on('click', '.toothTopRight>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .rightUp").val().replace(text, "");
            for(var i=0 ; i<inputValue.length ; i++){
                arr.push(inputValue.substring(i,i+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]<arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change;
                    }
                }
            };
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(".tips .rightUp").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            $(this).children().addClass('spanStyle');
            var add = $(".tips .rightUp").val() + text;
            for(var d=0 ; d<add.length ; d++){
                arr.push(add.substring(d,d+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]<arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change
                    }
                }
            };
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(".tips .rightUp").val(str);
        }
    });

    $(document).on('click', '.toothBottomLeft>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .leftDown").val().replace(text, "");
            for(var i=0 ; i<inputValue.length ; i++){
                arr.push(inputValue.substring(i,i+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]>arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change
                    }
                }
            };
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(".tips .leftDown").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            $(this).children().addClass('spanStyle');
            var add = $(".tips .leftDown").val() + text;
            for(var d=0 ; d<add.length ; d++){
                arr.push(add.substring(d,d+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]>arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change;
                    }
                }
            };
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(".tips .leftDown").val(str);
        }
    });

    $(document).on('click', '.toothBottomRight>span', function (e) {
        e.stopPropagation();
        var arr = [];
        var text = $(this).text();
        if ($(this).children().attr('class') == 'spanStyle') {
            var inputValue = $(".tips .rightDown").val().replace(text, "");
            for(var i=0 ; i<inputValue.length ; i++){
                arr.push(inputValue.substring(i,i+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]<arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change
                    }
                }
            }
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(".tips .rightDown").val(str);
            $(this).children().removeClass('spanStyle');
        } else {
            $(this).children().addClass('spanStyle');
            var add = $(".tips .rightDown").val() + text;
            for(var d=0 ; d<add.length ; d++){
                arr.push(add.substring(d,d+1));
            }
            for(var i=arr.length-1;i>=0;i--){
                for(var j=0;j<i;j++){
                    if(arr[j+1]<arr[j])  {
                        var change=arr[j+1];
                        arr[j+1]=arr[j];
                        arr[j]=change
                    }
                }
            }
            var str = "";
            for(var i=0 ; i<arr.length ; i++){
                str += arr[i];
            }
            $(".tips .rightDown").val(str);
        }
    });
});